const $form = this.pageObj.getApiWidgetByAliasName('monitor_form').widgetObj;
const monitorData = window._monitorSelect;
const variables = this.getApiPage().getVariables();
const proxy = variables.find(item => item.name === 'environmentType')?.value || '';

function request(url, body, method) {
  const headers = { "Content-Type": "application/json" };
  const option = { method, headers, body: JSON.stringify(body) };
  return fetch(url, option).then(res => res.json());
}

async function handle() {
  if (monitorData.operate === 'add') {
    const data = $form.getFormData('monitor_form');
    if (!data.viewName) return;
    request(`/${proxy}SummaryAssessmentSV/videoView/insert`, data, 'POST');
  }
  if (monitorData.operate === 'edit') {
    const data = $form.getFormData('monitor_form');
    if (!data.viewName) return;
    request(`/${proxy}SummaryAssessmentSV/videoView/update`, {...monitorData, ...data  }, 'POST');
  }
  if (monitorData.operate === 'delete') {
    request(`/${proxy}SummaryAssessmentSV/videoView/delete?id=${monitorData.id}`);
    const widgets = this.getApiPage().getApiWidgetByAliasName('三维组件');
    widgets.widgetObj.sullivan.closeAll();
  }
  renderMonitorData?.call(this);
  const modals = this.pageObj.getApiWidgetsByClassName('monitor-modal');
  modals.forEach(v => v.hideWidget());
}
handle.call(this);
